import { instanceToPlain } from "class-transformer";
class Caption {
  text: string = "";
}

class Photo {
  id: number | null = null;
  filename: string = "";
  user: User = new User();
  users: User[] = [];
  caption: Caption = new Caption();
}

class User {
  id: number | null = null;
  firstName: string = "";
  caption: Caption = new Caption();
  photos: Photo[] = [];
}

const photo1: Photo = new Photo();
photo1.id = 1;
photo1.filename = "me.jpg";

const photo2: Photo = new Photo();
photo2.id = 2;
photo2.filename = "she.jpg";

const caption: Caption = new Caption();
caption.text = "cool photo";

const user: User = new User();
user.caption = caption;
user.firstName = "Umed Khudoiberdiev";
user.photos = [photo1, photo2];

photo1.user = user;
photo2.user = user;
photo1.users = [user];
photo2.users = [user];

photo1.caption = caption;
photo2.caption = caption;

const plainUser = instanceToPlain(user, { enableCircularCheck: true });
console.log(JSON.stringify(plainUser, null, " "));
// 问题：class 成员必须初始化
// 期望 plainUser 与如下相等
// {
//     firstName: 'Umed Khudoiberdiev',
//     caption: { text: 'cool photo' },
//     photos: [
//       {
//         id: 1,
//         filename: 'me.jpg',
//         users: [],
//         caption: { text: 'cool photo' },
//       },
//       {
//         id: 2,
//         filename: 'she.jpg',
//         users: [],
//         caption: { text: 'cool photo' },
//       },
//     ],
//   }
